goog.require('dkpoints.nav.StandingsHandler');
goog.require('dkpoints.tests.stubFn');

/**
 * @fileoverview
 * Unit tests for dkpoints.nav.StandingsHandler.
 */


var bodyContent, rpc, status, stubFn;
function setUpPage() {
  bodyContent = document.getElementById('body_content');
  stubFn = dkpoints.tests.stubFn;
}


var handler = null;


function setUp() {
  rpc = {};
  status = {};
}


/**
 * Test that the constructor stores the given dependencies correctly.
 */
var testConstructorStoresDependencies = function() {

  handler = new dkpoints.nav.StandingsHandler(bodyContent, rpc, status);

  assertEquals('Handler.bodyContent_ should be the element we gave it.',
               bodyContent, handler.bodyContent_);
  assertEquals('Handler.rpc_ should be the object we gave it.',
               rpc, handler.rpc_);
  assertEquals('Handler.status_ should be the object we gave it.',
               status, handler.status_);
};


/**
 * Test that StandingsHandler declares that it needs bodyContent.
 */
var testDisposePreviousSet = function() {
  handler = new dkpoints.nav.StandingsHandler(bodyContent, rpc, status);
  assertTrue('Handler.disposePrevious needs to be true.',
             handler.disposePrevious);
};


/**
 * Test that StandingsHandler creates a new Standings presenter on the first
 * call to handle() and reuses the same one for subsequent calls.
 */
var testHandleCreatesAndReusesStandingsPresenter = function() {
  var realStandings = dkpoints.standings.Standings;

  var constructorCalls = 0, renderCalls = 0;
  var std = dkpoints.standings.Standings = function() {
    ++constructorCalls;
  };
  std.prototype.render = function() { ++renderCalls; };

  handler = new dkpoints.nav.StandingsHandler(bodyContent, rpc, status);
  handler.handle({});

  var standings = handler.standings_;
  assertEquals('Standings constructor should have been called exactly once.',
               1, constructorCalls);
  assertEquals('Standings.render should have been called exactly once.',
               1, renderCalls);

  handler.handle();
  assertEquals('Standings constructor should not have been called again.',
               1, constructorCalls);
  assertEquals('Standings.render should have been called exactly twice.',
               2, renderCalls);
  assertEquals('The second handle() should use the same Standings object.',
               standings, handler.standings_);

  dkpoints.standings.Standings = realStandings;
};


/**
 * Test that calling exit on StandingsHandler calls dispose on the
 * Standings presenter.
 */
var testExitDisposesPresenter = function() {
  var disposeCalled = false;

  handler = new dkpoints.nav.StandingsHandler(bodyContent, rpc, status);
  handler.standings_ = {
    dispose: stubFn()
  };

  var callback = stubFn();

  handler.exit(callback);
  assertTrue('Dispose should have been called on the Standings presenter.',
             handler.standings_.dispose.called);
  assertTrue('The onExit callback should have been called.',
             callback.called);
};


function tearDown() {
  handler = null;
}
